﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace WindowsGame1
{
    class Camera
    {
        public Vector2 m_Position;
        public BaseObject m_Target;
        public Vector2 m_TargetPrev;
        public float m_fZoom;

        public Camera()
        {
            m_fZoom = 1;
            m_TargetPrev = new Vector2(0.0f, 0.0f);
        }

        public void Update(float deltaT)
        {
            m_fZoom = 1.0f;
            Vector2 Window;
            if (GlobalContentManager.Instance.Graphics.IsFullScreen)
                Window = new Vector2(GlobalContentManager.Instance.Graphics.GraphicsDevice.DisplayMode.Width, GlobalContentManager.Instance.Graphics.GraphicsDevice.DisplayMode.Height);
            else
                Window = new Vector2(GlobalContentManager.Instance.Graphics.PreferredBackBufferWidth, GlobalContentManager.Instance.Graphics.PreferredBackBufferHeight);
            //m_fZoom = Window.X / GlobalContentManager.Instance.Graphics.PreferredBackBufferWidth;
            
            if (m_Target != null)
                m_Position = m_Target.m_Position;
            m_Position.X -= (Window.X / 2.0f);
            m_Position.Y -= (Window.Y / 2.0f);

            if ((LevelManager.Instance.m_MapSize.X) * LevelManager.Instance.m_TileSize.X > Window.X)
            {
                if (m_Position.X < 0.0f)
                    m_Position.X = 0.0f;
                if (m_Position.X > ((LevelManager.Instance.m_MapSize.X) * LevelManager.Instance.m_TileSize.X * m_fZoom) - Window.X / m_fZoom)
                    m_Position.X = ((LevelManager.Instance.m_MapSize.X) * LevelManager.Instance.m_TileSize.X * m_fZoom) - Window.X / m_fZoom;
            }
            else
            {
                m_Position.X = (int)((LevelManager.Instance.m_MapSize.X * LevelManager.Instance.m_TileSize.X) / 2.0f) - (int)(Window.X/2.0f);
            }
            if ((LevelManager.Instance.m_MapSize.Y) * LevelManager.Instance.m_TileSize.Y > Window.Y)
            {
                if (m_Position.Y < 0.0f)
                    m_Position.Y = 0.0f;
                if (m_Position.Y > (((LevelManager.Instance.m_MapSize.Y - 1)) * LevelManager.Instance.m_TileSize.Y * m_fZoom) - Window.Y / m_fZoom)
                    m_Position.Y = (((LevelManager.Instance.m_MapSize.Y - 1)) * LevelManager.Instance.m_TileSize.Y * m_fZoom) - Window.Y / m_fZoom;
            }
            else
            {
                m_Position.Y = (int)((LevelManager.Instance.m_MapSize.Y * LevelManager.Instance.m_TileSize.Y) / 2.0f) - (int)(Window.Y / 2.0f);
            }

            if (m_Target != null)
                m_TargetPrev = m_Target.m_Position;
        }
    }
}
